package cn.dhnapplets.controller;

import cn.dhnapplets.entity.MerchanData;
import cn.dhnapplets.entity.PackageOrder;
import cn.dhnapplets.entity.Result;

import cn.dhnapplets.service.MerchantDataService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @Author 邱文彬
 * @Date 2021/12/1 8:34
 * @Version 1.0
 */


@RestController
@RequestMapping(value = "/merchant/delivery" ,produces="application/json;charset=UTF-8")
public class MerchanDataController {


    @Autowired
    private MerchantDataService merchantService;

    @ApiOperation("根据商家名称查询数据")
    @RequestMapping(value = "getAll",method = RequestMethod.POST)
    public Result getAll(@RequestBody MerchanData merchanData){

//        判断传入的openId是否为空
        if(merchanData.getOpenId().equals("")){
            return new Result(0,"传入的商家名为空",null);
        }

        String name = merchantService.getOpenId(merchanData.getOpenId());
        PackageOrder packageOrder = new PackageOrder();
        if(merchanData.getCode() == 0){
            packageOrder =   getToday(name);
        }
        if(merchanData.getCode() == 1){
            packageOrder =   getSevenDays(name);
        }
        if(merchanData.getCode() == 2){
            packageOrder =   getThirtyDays(name);
        }
        if(merchanData.getCode() == 3){
            packageOrder =  getOneYear(name);
        }if(merchanData.getCode() == 4){
            packageOrder = merchantService.getAll(name);
        }




        if(packageOrder == null){
            return new Result(0,"查询时发生未知错误",null);
        }

        BigDecimal num = new BigDecimal(packageOrder.getAmount());
        packageOrder.setAmount(num.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        return new Result(0,"查询成功",packageOrder);


    }
    public PackageOrder getToday( String recycleName){

        return merchantService.getToday(recycleName);
    }

    public PackageOrder getSevenDays(@RequestBody String recycleName){

        return merchantService.getSevenDays(recycleName);
    }
    public PackageOrder getThirtyDays(@RequestBody String recycleName){

        return merchantService.getThirtyDays(recycleName);
    }
    public PackageOrder getOneYear(@RequestBody String recycleName){
        return merchantService.getOneYear(recycleName);
    }
}
